﻿/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xoskit.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#include "../import/head.h"
#include "../global/head.h"
#include "../xos/head.h"
#include "thread_impl.h"

/////////////////////////////////////////////////////////////////////////////////////////
// 
namespace xos
{
    ////////////////////////////////////////////////////////////////////////////////////
    // 

    static thread_impl::POOL * pool_ptr = 0;

    ////////////////////////////////////////////////////////////////////////////////////
    //

    thread_impl::thread_impl()
    {
        m_thread_fun = 0;
        m_lpData = 0;
        m_id = 0;
    }

    thread_impl::~thread_impl()
    {
        term();
    }

    ////////////////////////////////////////////////////////////////////////////////////
    // 

    int thread_impl::get_addr( POOL *** pppPool, LIST *** pppList )
    {
        int ret = 0;
        if( pppPool )
        {
            *pppPool = &pool_ptr;
        }
        if( pppList )
        {
            *pppList = 0;
        }
        return ret;
    }

    int thread_impl::init()
    {
        int nRet = 0;
        return nRet;
    }

    int thread_impl::term()
    {
        int nRet = 0;

        m_thread_fun = 0;
        m_lpData = 0;
        m_id = 0;

        return nRet;
    }

    /////////////////////////////////////////////////////////////////////////////////////////////
    // 
    // create thread
    // 
    int thread_impl::create( void * ( * f )( void * ), void * lpData )
    {
        int nRet = 0;

        m_thread_fun = f;
        m_lpData = lpData;
        nRet = pthread_create( &m_id, 0, m_thread_fun, m_lpData );

        return nRet;
    }

    // 
    // wait thread to terminate..
    // 
    int thread_impl::wait()
    {
        int nRet = 0;

        if( 0 != m_id )
        {
            pthread_join( m_id, 0 );
            m_id = 0;
        }

        return nRet;
    }
} // xos
